using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

public static class StatusOut
{
	// default constructor
	static StatusOut()
	{
		if (!Directory.Exists("Saves"))
			Directory.CreateDirectory("Saves");
	}

	public static void SaveLoadOut(bool save)
	{
		using (StreamWriter op = new StreamWriter("Saves/SaveLoad.txt"))
		{
			// headers
			op.Write("SaveLoad" + "|");
			op.Write("Fleed ID" + "|");
			op.Write("Name" + "|");
			op.Write("Kingdom" + "|");
			op.Write("Class" + "|");
			op.Write("Type" + "|");
			op.Write("Rank" + "|");
			op.Write("Cost" + "|");
			op.Write("Location" + "|");
			op.Write("Home" + "|");
			op.Write("Dest" + "\n");

			foreach (Fleet fl in Core.Fleets.Values)
			{
				op.Write(save ? "Save" : "Load" + "|");
				op.Write(fl.FleetID.ToString() + "|");
				op.Write(fl.ShipName + "|");
				op.Write(fl.Belong.Race.RaceType.ToString() + "|");
				op.Write(fl.FleetClass.ToString() + "|");
				op.Write(fl.GetType().ToString() + "|");
				op.Write(fl.SortRank.ToString() + "|");
				op.Write(fl.BuildCost.ToString() + "|");
				op.Write(fl.Location.ToString() + "|");
				op.Write(fl.Home == null ? "" : fl.Home.StarName + " " + fl.Home.Location.ToString() + "|");
				op.Write(fl.Dest == null ? "" : fl.Dest.StarName + " " + fl.Dest.Location.ToString() + "\n");
			}
		}
	}

	public static void SendKingdomsOut()
	{
		using (StreamWriter op = new StreamWriter("Saves/kingdoms.txt"))
		{
			// headers
			op.Write("Race" + "|");
			op.Write("King" + "|");
			op.Write("Home" + "|");
			op.Write("Gold" + "|");
			op.Write("Science" + "|");
			op.Write("Reach" + "|");
			op.Write("Command" + "|");
			op.Write("Heat" + "|");
			op.Write("Type" + "|");
			op.Write("Orbit" + "|");
			op.Write("Planet" + "|");
			op.Write("Auto" + "|");
			op.Write("Size" + "|");
			op.Write("Climate" + "|");
			op.Write("Mineral" + "|");
			op.Write("Lifeform" + "|");
			op.Write("Developing" + "|");
			op.Write("Developed" + "|");
			op.Write("BaseDev" + "|");
			op.Write("BaseProd" + "|");
			op.Write("BaseFarm" + "|");
			op.Write("Marine" + "|");
			op.Write("BaseDef" + "|");
			op.Write("MaxPop" + "|");
			op.Write("Population" + "\n");

			foreach (Kingdom kd in Core.galaxyKingdoms)
				foreach (Kingdom.SolarPlanet sp in kd.Planets)
					foreach (Planet pl in sp.plPlanets)
					{
						string fa = "";
						string owned = "";
						double pop = pl.CalPopulation();
						int cnt = 0;

						foreach (Planet.PlanetFacility pf in pl.Facilities)
							if (pf.pInEffect)
							{
								cnt++;
								owned += pf.pBuild.DispName + ",";
							}

						if (pl.DevObj.Count > 0)
							fa = ((IBuildable)pl.DevObj[0]).DisplayName;

						// kingdom
						op.Write(kd.Race.RaceType.ToString() + "|");
						op.Write(kd.KingName + "|");
						op.Write(kd.Home.Location.ToString() + "|");
						op.Write(kd.Gold.ToString() + "|");
						op.Write(kd.SciPower.ToString() + "|");
						op.Write(kd.Reach.ToString() + "|");
						op.Write(kd.Command.ToString() + "|");
						// solar
						op.Write(sp.pSolar.Heat.ToString() + "|");
						op.Write(sp.pSolar.Type.ToString() + "|");
						op.Write(sp.pSolar.NumOrbits.ToString() + "|");
						// planet
						op.Write(pl.PlanetName + "|");
						op.Write(pl.AutoMode.ToString() + "|");
						op.Write(pl.Size.ToString() + "|");
						op.Write(pl.Climate.ToString() + "|");
						op.Write(pl.Mineral.ToString() + "|");
						op.Write(pl.Lifeform.ToString() + "|");
						op.Write(pl.DevObj.Count.ToString() + "|");
						op.Write(cnt.ToString() + "," + owned + "|");
						op.Write((pl.BaseDev + pl.DevPower).ToString() + "|");
						op.Write((pl.BaseProd + pl.ProdPower).ToString() + "|");
						op.Write((pl.BaseFarm + pl.FarmPower).ToString() + "|");
						op.Write(pl.BaseMarine.ToString() + "|");
						op.Write(pl.BaseDefense.ToString() + "|");
						op.Write(pl.MaxPop.ToString() + "|");
						op.Write(pop.ToString() + "\n");
					}
		}
	}

	public static void SendTechNamesOut()
	{
		using (StreamWriter op = new StreamWriter("Saves/technology.txt"))
		{
			// headers
			op.Write("TechName" + "|");
			op.Write("Category" + "|");
			op.Write("Stage" + "|");
			op.Write("Cost" + "|");
			op.Write("Key" + "|");
			op.Write("Parent" + "\n");

			foreach (Technology tech in Technology.Table)
			{
				string depend = "";

				foreach (GameEnums.TechName tn in tech.Dependent)
					depend += tn.ToString() + ", ";

				if (depend.Length > 0)
					depend = depend.Substring(0, depend.Length - 2);

				op.Write(tech.Name.ToString() + "|");
				op.Write(tech.Category.ToString() + "|");
				op.Write(tech.Stage.ToString() + "|");
				op.Write(tech.Cost.ToString() + "|");
				op.Write((tech.Key ? "Yes" : "No") + "|");
				op.Write(depend + "\n");
			}
		}

	}

	public static void SendPlanetsOut(List<StarSystem> slist)
	{
		using (StreamWriter op = new StreamWriter("Saves/stars.txt"))
		{
			Point3D pt;
			double distance;
			string pflag;
			string nflag;
			string sGiant;

			// headers
			op.Write("StarName" + "|");
			op.Write("Location" + "|");
			op.Write("Type" + "|");
			op.Write("Distance" + "|");
			op.Write("StarID" + "|");
			op.Write("PlanetName" + "|");
			op.Write("PlanetID" + "|");
			op.Write("Size" + "|");
			op.Write("Is Gas?" + "|");
			op.Write("Heat" + "|");
			op.Write("Climate" + "|");
			op.Write("Mineral" + "|");
			op.Write("Lifeform" + "|");
			op.Write("Orbit" + "|");
			op.Write("PlanetFlag" + "|");
			op.Write("NativesFlag" + "\n");

			foreach (StarSystem ss in slist)
			{
				pt = ss.Location;
				distance = Math.Sqrt(Math.Pow(pt.X, 2) + Math.Pow(pt.Y, 2) + Math.Pow(pt.Z, 2));

				foreach (Planet pl in ss.Planets)
				{
					sGiant = pl.Giant ? "Yes" : "No";
					pflag = Convert.ToString((int)pl.PlanetFlag, 2);
					nflag = Convert.ToString((int)pl.NativesFlag, 2);

					op.Write(ss.StarName + "|");
					op.Write(pt.ToString() + "|");
					op.Write(ss.Type + "|");
					op.Write(((int)distance).ToString() + "|");
					op.Write(ss.StarID.ToString() + "|");
					op.Write(pl.PlanetName + "|");
					op.Write(pl.PlanetID + "|");
					op.Write(pl.Size + "|");
					op.Write(sGiant + "|");
					op.Write(ss.Heat + "|");
					op.Write(pl.Climate + "|");
					op.Write(pl.Mineral + "|");
					op.Write(pl.Lifeform + "|");
					op.Write(pl.Orbit + "|");
					op.Write("0x" + pflag + "|");
					op.Write("0x" + nflag + "\n");
				}
			}
		}
	}

}
